//
//  RansomOrderController.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/4.
//

#import "RansomOrderController.h"
#import "HandleNetProduct+Transaction.h"
#import "UIColor+NN.h"
#import "UserModel.h"
#import "CKPuslichTool.h"
#import "Header.h"
#import "RansomNewController.h"
#import "RansomOrderCell.h"
#import "NewRansomController.h"
#import "UIAlertView+ActionBlock.h"

@interface RansomOrderController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *ransomOrderTable;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraint;
@property (nonatomic, strong) RansomOrderDetModel *detModel;

@end

@implementation RansomOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"赎回订单";
    self.ransomOrderTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getRansomOrderDetailAction];
}

- (void)getRansomOrderDetailAction {
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    [HandleNetProduct getRansomOrderDetailWithCustomerId:mid RansomId:[_redeemId intValue] Success:^(NSArray * _Nonnull data) {
        if (data.count > 0) {
            _detModel = [RansomOrderDetModel mj_objectWithKeyValues:data.firstObject];
            if ([_detModel.IsEdite integerValue] == 1) {
                _editBtn.hidden = NO;
            }
            else {
                _editBtn.hidden = YES;
            }
            if ([_detModel.IsDelete integerValue] == 1) {
                _deleteBtn.hidden = NO;
            }
            else {
                _deleteBtn.hidden = YES;
            }
            
            if ([_detModel.RedeemStatus isEqualToString:@"作废"]) {
                _constraint.constant = 0;
                _bottomView.hidden = YES;
            }
            [_ransomOrderTable reloadData];
        }
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_detModel) {
        return 10;
    }
    else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RansomOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RansomOrderCell"];
    [cell setRansomOrderDetAction:_detModel index:indexPath.row];
    return cell;
}


- (IBAction)bottomBtnAction:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
        {
            NewRansomController *vc = (NewRansomController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"NewRansomController"];
            vc.redeemId = _redeemId;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定作废" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
                if (buttonIndex == 1) {
                    UserModel *model=[UserModel getUser];
                    int mid=[model.CustomerID intValue];
                    [HandleNetProduct deleteRansomOrderWithCustomerId:mid RansomId:[_redeemId intValue] Success:^(NSArray * _Nonnull data) {
                        [self.navigationController popViewControllerAnimated:YES];
                    } Failure:^(NSString * _Nonnull errorInfo) {
                        
                    }];
                }
            };
            [alert show];
        }
            break;
        case 2:
        {
            RansomNewController *vc = (RansomNewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomNewController"];
            vc.isDetail = YES;
            vc.redeemId = _redeemId;
            if ([_detModel.RedeemStatus isEqualToString:@"赎回成功"]) {
                vc.state = YES;
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}

@end
